package vip.xiaonuo.modular.util;

public enum ResultCode implements IResultCode {

    /**
     *  失败状态码
     */
    FAILURE(-1, "失败"),
    /**
     *  成功状态码
     */
    SUCCESS(200, "成功"),
    ;

    private int code;

    private String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public int getCode() {
        return this.code;
    }

    @Override
    public String getMsg() {
        return this.message;
    }

    public ResultCode format(Object... msgArgs) {
        this.message = String.format(this.message, msgArgs);
        return this;
    }

    public static String getMessage(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name() + " : " + this.code + ", " +this.message;
    }

}
